import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../components/Home.vue";
const routes = [{
        name: "home",
        path: "/",
        meta: {
            title: "首页",
        },
        component: Home,
        redirect: "/welcome",
        children: [{
                name: "welcome",
                path: "welcome",
                component: () => import("../views/Welcome.vue"),
                meta: {
                    title: "欢迎页",
                },
            },
            {
                name: "user",
                path: "/system/user",
                component: () => import("../views/User.vue"),
                meta: {
                    title: "用户管理",
                }
            },
            {
                name: "menu",
                path: "/system/menu",
                component: () => import("@/views/Menu.vue"),
                meta: {
                    title: "菜单管理"
                }
            },
            {
                name: "role",
                path: "/system/role",
                component: () => import("@/views/Role.vue"),
                meta: {
                    title: "角色管理"
                }
            },
            {
                name: "dept",
                path: "/system/dept",
                component: () => import("@/views/Dept.vue"),
                meta: {
                    title: "部门管理"
                }
            },
            {
                name: "leave",
                path: "/audit/leave",
                component: () => import("@/views/Leave.vue"),
                meta: {
                    title: "休假申请"
                }
            },
            {
                name: "approve",
                path: "/audit/approve",
                component: () => import("@/views/Approve.vue"),
                meta: {
                    title: "待我审批"
                }
            }
        ],
    },
    {
        name: "login",
        path: "/login",
        meta: {
            title: "登录页",
        },
        component: () => import("../views/Login.vue"),
    },
    {
        name: "404",
        path: "/404",
        meta: {
            title: "页面不存在",
        },
        component: () => import("../views/404.vue"),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    if (router.hasRoute(to.name)) {
        document.title = to.meta.title
        next()
    } else {
        next('/404')
    }
})
export default router;